package GUI;

import 学生.Student;
import 数据库和网络.myDatebase;
import 试题和试卷.paper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class revisePaperItem extends JFrame {
    JLabel num,paperLabel;
    JButton reviseButton;
    public paper currentpaper;
    public Student currentstudent;
    static revisePaperItem revisePaper;

public revisePaperItem(int n, paper currentpaper){
            currentstudent=revisePaperChooseWindow.currentstudent;
            this.currentpaper=currentpaper;
            num = new JLabel(""+n);
            int loc ;
            if(n%5!=0){
                loc=n%5;
            }else {
                loc=5;
            }
            setTitleType(num);
            paperLabel = new JLabel("试卷名："+currentpaper.getName()+"  ");
            setTextType(paperLabel);
            num.setBounds(80,120+50*loc,60,50);
            paperLabel.setBounds(140,120+50*loc,600,50);
            reviseButton = new JButton("批改试卷");

            setButtonType(reviseButton);
            reviseButton.setBounds(740,120+50*loc,80,50);

            reviseButton.addActionListener(new reviseListener());


        }
    public class reviseListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e){
            myDatebase finishQuery=new myDatebase("result_record");
            String sql;
            int timezone=1;
            String isrevised="";
            try {
                //sql="select remainTime,studentID from result_record.'再来一次！'where studentID='"+currentstudent.getSchoolID()+"'";
                sql=String.format("select remainTime,studentID,is_revised from result_record.%s where studentID='"+currentstudent.getSchoolID()+"'",currentpaper.getName());
                finishQuery.statement.execute(sql);
                finishQuery.resultSet=finishQuery.statement.executeQuery(sql);
                while(finishQuery.resultSet.next()){
                    timezone=finishQuery.resultSet.getInt("remainTime");
                    isrevised=finishQuery.resultSet.getString("is_revised");
                }
                if(timezone!=0||(isrevised != null &&!isrevised.equals("yes")) ){
                    JFrame unfinished=new JFrame();
                    JPanel panel = new JPanel(new GridLayout(4, 1,20,20));
                    JLabel successLab=new JLabel();
                    JLabel lab2=new JLabel(" ");
                    unfinished.setSize(200,150);
                    Font bigFont = new Font("sanserif",Font.PLAIN,15);
                    String a="该学生尚未完成！";
                    successLab.setText(a);
                    successLab.setFont(bigFont);
                    panel.setSize(unfinished.getSize());
                    panel.add(lab2);
                    panel.add(successLab);
                    unfinished.getContentPane().add(BorderLayout.CENTER,panel);
                    unfinished.setLayout(new FlowLayout());//流式布局
                    unfinished.setLocationRelativeTo(null);//居中

                    unfinished.setTitle("在线考试平台");
                    unfinished.setVisible(true);
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            new reviseWindow(currentpaper,currentstudent);
        }
    }
    public void setTitleType(JLabel label){
        Font bigFont = new Font("sanserif",Font.PLAIN,16);
        label.setBackground(Color.lightGray);
        label.setFont(bigFont);
        label.setOpaque(true);
        label.setBorder(BorderFactory.createLineBorder(Color.darkGray));
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }
    public void setTextType(JLabel label){
        Font smallFont = new Font("sanserif",Font.PLAIN,12);
        label.setBackground(Color.white);
        label.setOpaque(true);
        label.setFont(smallFont);
        label.setBorder(BorderFactory.createLineBorder(Color.lightGray));
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }
    public void setButtonType(JButton button){
        Font smallFont = new Font("sanserif",Font.PLAIN,14);
       // button.setBackground(Color.GRAY);
        button.setFont(smallFont);
        button.setBorder(BorderFactory.createLineBorder(Color.darkGray));
        button.setHorizontalAlignment(SwingConstants.CENTER);
    }

}


